package com.hxtec.polarislogin.polarislogin.config;

import com.hxtec.polarislogin.polarislogin.VerifyService;
import com.hxtec.polarislogin.polarislogin.dao.UserDao;
import com.hxtec.polarislogin.polarislogin.entity.ShopUserEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author Marcus lv
 * @create 2019/11/17 4:36 下午
 */
@Service

public class UserDetailServiceImpl implements UserDetailsService {
    private static final Logger log = LoggerFactory.getLogger(UserDetailServiceImpl.class);

    private final UserDao userDao;
    private final VerifyService verifyService;
    private BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();


    public UserDetailServiceImpl(UserDao userDao, VerifyService verifyService) {
        this.userDao = userDao;
        this.verifyService = verifyService;
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        log.info("login {}",s);
        String[] split = s.split("/");
        String type = split[0];
        String username = split[1];

        List<GrantedAuthority> list = new ArrayList<>();
        list.add(new SimpleGrantedAuthority("user"));

        switch (type) {
            case "i":
                return admin(username, list);
            case "p":
                return userpass(username, list);
            case "w":
                return wx(username, list);
            default:
                return fuck();
        }
    }

    private UserDetails fuck() {
        throw new UsernameNotFoundException("not found");
    }

    private UserDetails wx(String username, List<GrantedAuthority> list) {
        Optional<ShopUserEntity> shopUserEntity = userDao.findByWxId(username);
        String password = "";
        boolean userStatus;
        if (shopUserEntity.isPresent()) {
            final ShopUserEntity userEntity = shopUserEntity.get();
            userStatus = "0".equals(userEntity.getState());
            password = encoder.encode(username);
        } else {
            throw new UsernameNotFoundException("not found");
        }
        return new User(username, password, userStatus, true, true, true, list);
    }

    private UserDetails userpass(String username, List<GrantedAuthority> list) {
        Optional<ShopUserEntity> shopUserEntity = userDao.findByMobile(username);
        boolean userStatus = false;
        if (shopUserEntity.isPresent()) {
            final ShopUserEntity userEntity = shopUserEntity.get();
            userStatus = "0".equals(userEntity.getState());
        } else {
            ShopUserEntity user = new ShopUserEntity();
            user.setMobile(username);
            user.setPassword(null);
            user.setState("0");
            user.setIsDelete("N");
            userDao.save(user);
            userStatus=true;
        }
        String password = encoder.encode(String.valueOf(verifyService.verify(username)));
        return new User(username, password, userStatus, true, true, true, list);
    }

    private UserDetails admin(String username, List<GrantedAuthority> list) {
        list.add(new SimpleGrantedAuthority("admin"));
        return new User("admin", encoder.encode("123456"), true, true, true, true, list);
    }
}
